package sas

import (
	"time"

	"gitee.com/micro-plat/sas/sas/modules/const/enum"
	"gitee.com/micro-plat/sas/sas/modules/const/models"
	"github.com/micro-plat/lib4go/types"
)

//ConfOption 配置选项
type ClientOption func(*sasClient)

//WithKeyValue 设置键值连接符
func WithKeyValue(keyValue string) ClientOption {
	return func(n *sasClient) {
		n.keyValue = keyValue
	}
}

//WithChain 设置每组键值连接符
func WithChain(chain string) ClientOption {
	return func(n *sasClient) {
		n.chain = chain
	}
}

//WithSortAll 设置参数排序方式 排序方式 all-密钥和数据都要参与排序,必须设置密钥key名称;
func WithSortAll() ClientOption {
	return func(n *sasClient) {
		n.sort = enum.All
	}
}

//WithSortData 设置参数排序方式 排序方式 data-仅数据参与排序
func WithSortData() ClientOption {
	return func(n *sasClient) {
		n.sort = enum.Data
	}
}

//WithSortStatic 设置参数排序方式 排序方式 static--指定字段参与排序
func WithSortStatic(fields ...string) ClientOption {
	return func(n *sasClient) {
		n.sort = enum.Static
		n.fields = fields
	}
}

//WithSecretName 设置密钥键名(默认为空) 字段排序方式为:all,密钥也需要加入原串排序时,必须设置密钥key名称;如果不设置,默认:secert
func WithSecretName(sertName, sertKeyValue string) ClientOption {
	return func(n *sasClient) {
		n.sertName = sertName
		n.sertKeyValue = sertKeyValue
	}
}

//WithSertModeTail 设置密钥尾部拼接方式 并且用chain链接
func WithSertModeTail(chain ...string) ClientOption {
	return func(n *sasClient) {
		n.sertChain = types.GetStringByIndex(chain, 0, "")
		n.sertMode = enum.Tail
	}
}

//WithSertModeHead 设置密钥头部拼接方式 并且用chain链接
func WithSertModeHead(chain ...string) ClientOption {
	return func(n *sasClient) {
		n.sertChain = types.GetStringByIndex(chain, 0, "")
		n.sertMode = enum.Head
	}
}

//WithSertModeTailNadHead 设置密钥头尾部拼接方式 并且用chain链接
func WithSertModeTailAndHead(chain ...string) ClientOption {
	return func(n *sasClient) {
		n.sertChain = types.GetStringByIndex(chain, 0, "")
		n.sertMode = enum.HeadTail
	}
}

//WithUIDAlias 设置用户euid的字段名
func WithUIDAlias(name string) ClientOption {
	return func(n *sasClient) {
		n.alias[enum.EUIDName] = name
	}
}

//WithTimestampAlias 设置timestamp的字段名
func WithTimestampAlias(name string) ClientOption {
	return func(n *sasClient) {
		n.alias[enum.TimestampName] = name
	}
}

//WithSignAlias 设置sign的字段名
func WithSignAlias(name string) ClientOption {
	return func(n *sasClient) {
		n.alias[enum.SignName] = name
	}
}

//WithEncryptName 设置需要加密的字段名
func WithEncryptName(name ...string) ClientOption {
	return func(n *sasClient) {
		n.encryptName = append(n.encryptName, name...)
	}
}

//WithDynamicKeyName 设置动态密钥字段名
func WithDynamicKeyName(name string) ClientOption {
	return func(n *sasClient) {
		n.alias[enum.DynamicKeyName] = name
	}
}

//SecretOption 密码选项
type SecretOption func(*models.Item)

//WithExpire 过期时间
func WithExpire(expireSeconds int) SecretOption {
	return func(i *models.Item) {
		i.ExpireTime = time.Now().Add(time.Second * time.Duration(expireSeconds)).Format("2006-01-02 15:04:05")
	}
}

//WithPkcsType 编码方式 pkcs1,pkcs7,pkcs8
func WithPkcsType(pkcsType string) SecretOption {
	return func(i *models.Item) {
		i.RsaType = pkcsType
	}
}

//WithRsaBits 编码方式 1024/2048
func WithRsaBits(rsaBits int) SecretOption {
	return func(i *models.Item) {
		i.RsaBits = rsaBits
	}
}

//WithRsaPrivateKey 私钥
func WithRsaPrivateKey(priKey string) SecretOption {
	return func(i *models.Item) {
		i.RsaPrivateSecret = priKey
	}
}

//WithRsaPublicKey 公钥
func WithRsaPublicKey(pubKey string) SecretOption {
	return func(i *models.Item) {
		i.RsaPublicSecret = pubKey
	}
}

//WithAesSecret AES
func WithAesSecret(key string) SecretOption {
	return func(i *models.Item) {
		i.AesSecret = key
	}
}

//WithDesSecret DES
func WithDesSecret(key string) SecretOption {
	return func(i *models.Item) {
		i.DesSecret = key
	}
}

//WithHmacSecret  HmacSecret
func WithHmacSecret(key string) SecretOption {
	return func(i *models.Item) {
		i.HmacSecret = key
	}
}

//WithMd5Secret  Md5Secret
func WithMd5Secret(key string) SecretOption {
	return func(i *models.Item) {
		i.Md5Secret = key
	}
}

//WithSha1Secret  Hmac
func WithSha1Secret(key string) SecretOption {
	return func(i *models.Item) {
		i.Sha1Secret = key
	}
}

//WithSha256Secret  Sha256Secret
func WithSha256Secret(key string) SecretOption {
	return func(i *models.Item) {
		i.Sha256Secret = key
	}
}

//WithSha512Secret  Sha512Secret
func WithSha512Secret(key string) SecretOption {
	return func(i *models.Item) {
		i.Sha512Secret = key
	}
}

//MerchantCert  MerchantCert
func WithMerchantCert(key string) SecretOption {
	return func(i *models.Item) {
		i.MerchantCert = key
	}
}

//WithSupplierCert  SupplierCert
func WithSupplierCert(key string) SecretOption {
	return func(i *models.Item) {
		i.SupplierCert = key
	}
}

//WithRootCert  RootCert
func WithRootCert(key string) SecretOption {
	return func(i *models.Item) {
		i.RootCert = key
	}
}

//WithCertKey  CertKey
func WithCertKey(key string) SecretOption {
	return func(i *models.Item) {
		i.CertKey = key
	}
}
